package cosplay.controller;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cosplay.DAO.CompetitionDAOImpl;
import cosplay.beans.Competitions;
@Controller
public class CompetitionController extends MultiActionController {
	@Autowired
	private CompetitionDAOImpl competitiondao;
	
	@RequestMapping(value="addcompetition.htm",method=RequestMethod.GET)
	protected ModelAndView handelshowadd(HttpServletRequest req,HttpServletResponse res)
	throws Exception
	{
		ModelMap model=new ModelMap();
		model.addAttribute("target","addcompetition.htm");
		return new ModelAndView("addcompetition","model",model);
	}	
	
	@RequestMapping(value="addcompetition.htm",method=RequestMethod.POST)
	protected ModelAndView handelactadd(HttpServletRequest req,HttpServletResponse res)
	throws Exception
	{
		Competitions competition=this.getCompetitionsFromRequest(req);
		int id =competitiondao.add(competition);
		ModelMap model=new ModelMap();
		if (id>0)
		{
			competition.setId(id);
			model.addAttribute("competition",competition);
			model.addAttribute("message","succed");
		}
		else
		{
			model.addAttribute("message","failed");
		}
		model.addAttribute("target","addcompetition.htm");
		return new ModelAndView("addcompetition","model",model);
	}
	
	@RequestMapping (value="editcompetition.htm",method=RequestMethod.GET)
	protected ModelAndView handelshowedit (HttpServletRequest req, HttpServletResponse res)
	throws Exception
	{
		ModelMap model=new ModelMap();
		if(cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
		{
			model.addAttribute("competition",competitiondao.get(Integer.valueOf(req.getParameter("id")).intValue()));
			model.addAttribute("target","editcompetition.htm");
		}
		else
		{
			model.addAttribute("message","competition not exist!");
			model.addAttribute("target","editcompetition.htm");
		}
		return new ModelAndView("addcompetition","model",model);
	}
	
	@RequestMapping (value="editcompetition.htm",method=RequestMethod.POST)
	protected ModelAndView handelactedit (HttpServletRequest req, HttpServletResponse res)
	throws Exception
	{
		ModelMap model=new ModelMap();
		if(cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
		{
			int id=Integer.valueOf(req.getParameter("id")).intValue();
			Competitions competition=this.getCompetitionsFromRequest(req);
			boolean sucess=competitiondao.edit(competition, id);
			model.addAttribute("competition",competition);
			if(sucess)
			{
				model.addAttribute("message","sucess");
			}
			else
			{
				model.addAttribute("message","edit failed try again later.");
			}
			model.addAttribute("target","editcompetition.htm");
		}
		else
		{
			model.addAttribute("message","Information invalid!");
			model.addAttribute("target","editcompetition.htm");
		}
		return new ModelAndView("addcompetition","model",model);
	}
	
	@RequestMapping(value="competitionadmin.htm")
	protected ModelAndView handelshowadmin(HttpServletRequest req,HttpServletResponse res)
	throws Exception
	{
		ModelMap model=new ModelMap();
		model.addAttribute("message",req.getParameter("message"));
		model.addAttribute("competitions",competitiondao.getall());
		return new ModelAndView("competitionlist","model",model);
	}
	
	@RequestMapping(value="delcompetition.htm")
	protected ModelAndView handelactdel (HttpServletRequest req,HttpServletResponse res)
	throws Exception
	{
		if (cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
		{
			int id=Integer.valueOf(req.getParameter("id")).intValue();
			boolean sucess=competitiondao.delete(id);
			if (sucess)
			{
				res.sendRedirect("competitionadmin.htm?message=sucess");
				return null;
			}
			else
			{
				res.sendRedirect("competitionadmin.htm?message=failed");
				return null;
			}
		}
		else
		{
			res.sendRedirect("competitionadmin.htm?message=error");
			return null;
		}
	}

	
	@RequestMapping(value="activecompetition.htm")
	protected ModelAndView handelactactive (HttpServletRequest req,HttpServletResponse res)
	throws Exception
	{
		if (cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
		{
			int id=Integer.valueOf(req.getParameter("id")).intValue();
			boolean sucess=competitiondao.activate(id);
			if (sucess)
			{
				res.sendRedirect("competitionadmin.htm?message=sucess");
				return null;
			}
			else
			{
				res.sendRedirect("competitionadmin.htm?message=failed");
				return null;
			}
		}
		else
		{
			res.sendRedirect("competitionadmin.htm?message=error");
			return null;
		}
	}
	
	@RequestMapping(value="finishcompetition.htm")
	protected ModelAndView handelactfinish (HttpServletRequest req,HttpServletResponse res)
	throws Exception
	{
		if (cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
		{
			int id=Integer.valueOf(req.getParameter("id")).intValue();
			boolean sucess=competitiondao.finish(id);
			if (sucess)
			{
				res.sendRedirect("competitionadmin.htm?message=sucess");
				return null;
			}
			else
			{
				res.sendRedirect("competitionadmin.htm?message=failed");
				return null;
			}
		}
		else
		{
			res.sendRedirect("competitionadmin.htm?message=error");
			return null;
		}
	}
	
	private Competitions getCompetitionsFromRequest(HttpServletRequest req)
	{
		if (req.getParameter("name")==null)
			return null;
		if (!cosplay.Services.FileServices.Stringintcheck(req.getParameter("limit")))
			return null;
		if (!cosplay.Services.FileServices.Stringintcheck(req.getParameter("votelimit")))
			return null;
		if (!cosplay.Services.FileServices.Stringintcheck(req.getParameter("rank")))
			return null;
		Competitions competition=new Competitions();
		competition.setActive(false);
		competition.setDescription(req.getParameter("description"));
		SimpleDateFormat end =new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date enddate;
		try {
			enddate = end.parse(req.getParameter("enddate"));
			if (enddate!=null)
				competition.setEndDate(new Timestamp(enddate.getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		competition.setLimit(Integer.valueOf(req.getParameter("limit")).intValue());
		competition.setVotelimit(Integer.valueOf(req.getParameter("votelimit")).intValue());
		competition.setName(req.getParameter("name"));
		competition.setRank(Integer.valueOf(req.getParameter("rank")).intValue());
		SimpleDateFormat start =new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date startdate;
		try {
			startdate = start.parse(req.getParameter("startdate"));
			if (startdate!=null)
				competition.setStartDate(new Timestamp(startdate.getTime()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(cosplay.Services.FileServices.Stringintcheck(req.getParameter("id")))
			competition.setId(Integer.valueOf(req.getParameter("id")).intValue());
		return competition;
	}
}
